package GUI;

import GUI.table.LeilaoTableModel;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import sistemadeleiloes.dao.LanceDAO;
import sistemadeleiloes.dao.LeilaoDAO;
import sistemadeleiloes.dao.ProdutoDAO;
import sistemadeleiloes.model.Lance;
import sistemadeleiloes.model.Leilao;
import sistemadeleiloes.model.Produto;
import sistemadeleiloes.model.Usuario;

/**
 *
 * @author Pedro
 */
public final class Leiloes extends javax.swing.JFrame {

    Produto item = new Produto();
    Usuario u1;
    Lance lance = new Lance();
    List<Produto> lista = null;
    Lance result;
    String senha;
    ProdutoDAO itemDAO = new ProdutoDAO();
    LanceDAO lanceDAO = new LanceDAO();

    /**
     * Creates new form Leilao
     */
    public Leiloes(Usuario u) {
        initComponents();
        u1 = u;
        listar();
    }

    public void listar() {
        LeilaoDAO lDAO = new LeilaoDAO();
        List<Leilao> lista = null;
        try {
            lista = lDAO.pesquisar();
        } catch (Exception ex) {
            Logger.getLogger(FormLeilao.class.getName()).log(Level.SEVERE, null, ex);
        }
        PreencherTabela(lista);
    }

    public void PreencherTabela(List<Leilao> lista) {
        //DefaultTableModel modelo = (DefaultTableModel)jTable.getModel();
        //modelo.setNumRows(0);
        Vector colunas = new Vector();
        Vector linhas = new Vector();
        String cabecalho[] = {"ID", "Nome", "Descricao", "Valor Inicial", "Situacao"};
        try {
            int x;
            for (x = 0; x < cabecalho.length; x++) {
                colunas.addElement(cabecalho[x]);
            }
            x = 0;
            while (x < lista.size()) {
                if (obterLinha(lista.get(x)) == null) {
                    linhas.removeElement(obterLinha(lista.get(x)));
                    x++;
                } else {
                    linhas.addElement(obterLinha(lista.get(x)));
                    x++;
                }
            }
            DefaultTableModel modelo = new DefaultTableModel(linhas, colunas);
            JTable j = new JTable(modelo);
        } catch (Exception exception) {
            JOptionPane.showMessageDialog(null,
                    "Erro: " + exception,
                    "Consulta de itens",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    private Vector obterLinha(Leilao item) {
        Vector linha = new Vector();
        if ("a".equals(item.getStatus())) {
            linha.addElement(item.getId());
            linha.addElement(item.getProduto().getNome());
            linha.addElement(item.getProduto().getDescricao());
            linha.addElement(item.getValorInicial());
            linha.addElement(item.getDtInicio());
            linha.addElement(item.getStatus());
        } else {
            //linha.addElement(null);
            return null;
        }

        return linha;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu1 = new javax.swing.JMenu();
        jScrollPane2 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        btcarregar = new javax.swing.JToggleButton();
        jLabel5 = new javax.swing.JLabel();
        btMenu = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        btParticipar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbProdutos = new javax.swing.JTable();

        jMenu1.setText("jMenu1");

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(table);

        btcarregar.setText("Carregar Tabela");
        btcarregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btcarregarActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel5.setFont(new java.awt.Font("Goudy Stout", 1, 20)); // NOI18N
        jLabel5.setText("Leilão Virtual");

        btMenu.setText(" MENU PRINCIPAL");
        btMenu.setName("cmdCancelar"); // NOI18N
        btMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMenuActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 2, 10)); // NOI18N
        jLabel1.setText("Escolha o seu produto e dê um lance!");

        btParticipar.setText("PARTICIPAR");
        btParticipar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btParticiparActionPerformed(evt);
            }
        });

        tbProdutos.setModel(new LeilaoTableModel());
        tbProdutos.setColumnSelectionAllowed(true);
        jScrollPane1.setViewportView(tbProdutos);
        tbProdutos.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        if (tbProdutos.getColumnModel().getColumnCount() > 0) {
            tbProdutos.getColumnModel().getColumn(0).setHeaderValue("PRODUTO");
            tbProdutos.getColumnModel().getColumn(1).setHeaderValue("VALOR INICIAL");
            tbProdutos.getColumnModel().getColumn(2).setHeaderValue("HORA DE INCÍCIO");
        }

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btParticipar, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(87, 87, 87)
                        .addComponent(btMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(175, 175, 175))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(256, 256, 256))))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(133, 133, 133)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(292, 292, 292)
                        .addComponent(jLabel5)))
                .addGap(0, 133, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btMenu, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btParticipar, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btcarregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btcarregarActionPerformed
    }//GEN-LAST:event_btcarregarActionPerformed

    private void btMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMenuActionPerformed
        Leiloes.this.dispose();
        new MenuPrincipal().setVisible(true);
    }//GEN-LAST:event_btMenuActionPerformed

    private void btParticiparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btParticiparActionPerformed
        //new FormLeilao(u1, idLeilao);
    }//GEN-LAST:event_btParticiparActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormLeilao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormLeilao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormLeilao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormLeilao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                // new FormLeilao().setVisible(true);
                
            }
        });

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btMenu;
    private javax.swing.JButton btParticipar;
    private javax.swing.JToggleButton btcarregar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable table;
    private javax.swing.JTable tbProdutos;
    // End of variables declaration//GEN-END:variables
}
